<?php
namespace App\Adshopuser\Models;
use Common\BaseModel;
class BShopOrderRefund extends BaseModel
{
    public $res;
    public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_order_refund');
    }

    public function admin_get_list($params=[]){
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condtions = "r.type={$params['type']}";
        $sql="
        SELECT u.nickname,u.phone,ad.name as ad_name,ad.phone as ad_phone,ad.address,o.order_sn,o.end_price,sku.value_ids,o.goods_name,o.goods_img,r.goods_type,r.reasion,r.img,r.remark,r.shop_is_agree,r.created_at FROM `b_shop_order_refund` as r
left join b_shop_order as o on o.order_sn=r.order_sn
left join f_users as u on u.id=r.user_id
left join b_shop_goods_sku as sku on sku.id=o.goods_sku_id
left join b_shop_order_address as ad on ad.id=o.address_id
where $condtions
order by r.created_at desc
limit $page,$size";
        $items = $this->getDI()->get('dbMaster')->fetchAll($sql);
        $BShopSkuValueDb = new BShopSkuValue();
        foreach($items as $key=>$value){
            $items[$key]["phone"] =$value['phone'] ? substr_replace($value['phone'], '****', 3, 4) : "";
            $items[$key]['sku_value_name'] = $value['value_ids'] ? $BShopSkuValueDb->getValueName($value['value_ids']) : "";
            $items[$key]['img'] = $value['img'] ? explode(',',$value['img']) : [];
        }

        $sql_count="
        SELECT count(r.id) as count FROM `b_shop_order_refund` as r
left join b_shop_order as o on o.order_sn=r.order_sn
left join f_users as u on u.id=r.user_id
left join b_shop_order_address as ad on ad.id=o.address_id
where $condtions limit 1";
        $g_count = $this->getDI()->get('dbMaster')->fetchOne($sql_count);
        $get_list['total'] = isset($g_count['count']) && !empty($g_count['count']) ? (int)$g_count['count'] : 0;
        $get_list['items'] = $items;
        return $get_list;
    }

}
